package com.hdu.other.monotonyStack;

public class lc962 {
    class Solution {
        public int maxWidthRamp(int[] nums) {
            int len = nums.length;
            int[] stack = new int[len];
            int r = 0;
            for (int i = 0; i < len; i++) {
                if (r == 0 || nums[stack[r - 1]] > nums[i]) {
                    stack[r++] = i;
                }
            }

            int ans = 0;
            for (int i = len - 1; i >= 0; i--) {
                while (r > 0 && nums[stack[r - 1]] <= nums[i]) {
                    int index = stack[--r];
                    ans = Math.max(ans, i - index);
                }
            }
            return ans;
        }
    }
}
